// registry_client.js
import Base from "sdk-base";

class DineggRegistryClient extends Base {
	// @ts-ignore
	private _registered: Map<string, string[]>;
	// @ts-ignore
	private _options: any;

	constructor(options) {
		super({
			// 指定异步启动的方法
			initMethod: "init",
		});
		this._options = options;
		this._registered = new Map();
	}

	/**
	 * 启动逻辑
	 */
	async init() {
		this.ready(true);
	}

	/**
	 * 订阅
	 * @param {Object} reg
	 *   - {String} dataId - the dataId
	 * @param {Function}  listener - the listener
	 */
	subscribe(reg: { dataId: string }, listener) {
		const key = reg.dataId;
		this.on(key, listener);

		// const data = this._registered.get(key);
		// if (data) {
		// 	process.nextTick(() => listener(data));
		// }
	}

	/**
	 * 发布
	 * @param {Object} reg
	 *   - {String} dataId - the dataId
	 *   - {String} publishData - the publish data
	 */
	publish(reg: { dataId: string; data: any }) {
		const key = reg.dataId;
		this.emit(key, reg.data);
	}
}

export default DineggRegistryClient;
